﻿using Sidvall.UI.Dialogs;
using Sidvall.UI.ViewModels;
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;

namespace Sidvall.UI.Uwp.Dialogs
{
    public sealed partial class SecurityDialog
    {
        #region Public Members

        #region ShowAsync

        public async Task ShowAsync(LogOnDialogContext logOnContext, LogOffDialogContext logOffContext, SecurityControlViewModelSettings viewModelSettings, object owner)
        {
            if ((logOnContext == null) && (logOffContext == null))
                return;
            var page = owner as Windows.UI.Xaml.Controls.Page;
            ControlManager.SetHeight(this, page.ActualHeight, 300);
            ControlManager.SetWidth(this, page.ActualWidth, 450);
            await this.SecurityControl.InitializeContextAsync(logOnContext, logOffContext, viewModelSettings);
            await ShowAsync();
        }

        #endregion

        #endregion
        #region Events

        #region ContentDialog_PrimaryButtonClick

        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            args.Cancel = true;
            await this.SecurityControl.DoPrimaryActionAsync();
        }

        #endregion
        #region ContentDialog_SecondaryButtonClick

        private async void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            args.Cancel = true;
            await this.SecurityControl.DoSecondaryActionAsync();
        }

        #endregion

        #region SecurityControl_LogOnCompleted

        private void SecurityControl_LogOnCompleted(object sender, EventArgs e)
        {
            Hide();
        }

        #endregion
        #region SecurityDialog_Opened

        private async void SecurityDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
        {
            await this.SecurityControl.StartAsync();
        }

        #endregion

        #endregion
        #region Constructors

        public SecurityDialog()
        {
            this.InitializeComponent();

            this.Opened += SecurityDialog_Opened;
        }

        #endregion
    }
}
